<?php
class Poll_Api_Core extends Core_Api_Abstract {
    /**
    * Get friendship status
    * 
    * @param mixed $resource
    * @param mixed $user
    * @return int 0: not friend, 1: friends, 2: waiting approval
    */
    public function isFriend($resource, $user) {
        if ($resource instanceof User_Model_User && $user instanceof User_Model_User) {                                                                                                     
            $tb = Engine_Api::_()->getDbTable('Membership', 'User'); 
            $sl = $tb->select()->where('resource_id = ?', $resource->getIdentity())->where('user_id = ?', $user->getIdentity());
            $r  = $tb->fetchRow($sl);
            if ($r) {
                if ($r->active == 1 && $r->resource_approved == 1 && $r->user_approved == 1) return 1;
                else if ($r->resource_approved == 1 && $r->user_approved == 0) return 2;
            }
        }
        return 0;
    }
    
    public function getFriends($resource, $page = NULL) {
        $tb = Engine_Api::_()->getDbTable('Membership', 'User');
        $sl = $tb->select()->where('active = 1 AND resource_approved = 1 AND user_approved = 1')
                ->where('resource_id = ?', $resource->getIdentity());
        if ($page) {
            return Zend_Paginator::factory($sl)->setCurrentPageNumber($page);
        }
        return $tb->fetchAll($sl);    
    }
    
    public function pollSelect($params = array()) {
        $tb = Engine_Api::_()->getDbTable('Polls', 'Poll');
        $sl = $tb->select();
                
        if (isset($params['search'])) {
            $sl->where('title LIKE ?', "%{$params['search']}%");
        }
        
        if (isset($params['privacy'])) {
            if ($params['privacy'] == 'all') {
                if (isset($params['viewer'])) {
                    $uid = $params['viewer'];
                    $mTb = Engine_Api::_()->getDbTable('Membership', 'User');
                    $tmpSL = $mTb->select()
                        ->from(array('m' => $mTb->info('name')), 'user_id')
                        ->where('active = 1 AND resource_approved = 1 AND user_approved = 1')
                        ->where('resource_id = ?', $uid);
                    $sl->where("(privacy = 'friends' AND user_id IN ({$tmpSL->__toString()})) OR privacy = 'public' OR user_id = $uid");
                }
            } else if ($params['privacy'] == 'friends') {
                $uid = $params['viewer'];
                $mTb = Engine_Api::_()->getDbTable('Membership', 'User');
                $tmpSL = $mTb->select()
                    ->from(array('m' => $mTb->info('name')), 'user_id')
                    ->where('active = 1 AND resource_approved = 1 AND user_approved = 1')
                    ->where('resource_id = ?', $uid);
                $sl->where("user_id IN ({$tmpSL->__toString()})");    
            } 
            else $sl->where('privacy = ?', $params['privacy']);
        }
        if (isset($params['feature'])) $sl->where('feature = ?', $params['feature']);
        if (isset($params['users'])) $sl->where('user_id IN (?)', $params['users']);
        if (isset($params['user_id'])) $sl->where('user_id = ?', $params['user_id']);
        if (isset($params['limit'])) $sl->limit($params['limit']);
        if (isset($params['order'])) $sl->order($params['order']);
        else $sl->order('poll_id DESC');
        
        return $sl;
    }
    
    public function pollPaginator($params = array()) {
        return Zend_Paginator::factory($this->pollSelect($params));
    }
}   
